#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <fstream>
using namespace std;
 typedef struct node
{
	char c;
	double weight;
}Node;

bool compNode(Node a,Node b)
{
	return a.weight > b.weight;
}

double str2Dou(char* str, int res[])
{
	double result=0;
	char *p = str;
	while( *p )
	{
		result = result*10 + res[*p-'A'];
		p++;
	}
	printf("%.0lf\n", result);
	return result;
}

int main()
{
	
//	printf("%.0lf", 1.234);
	char str[50][13];
    Node counter[10];
    double res;
    int strLeng[50], result[10];
	int n;
	fstream fin("input.txt");
	while( /*scanf( "%d", &n )!= EOF*/fin>>n ) // n<=50 
	{
		for(int i=0; i<n; i++)
		{
			//scanf( "%s", str[i] );
			fin>>str[i];
			strLeng[i] = strlen(str[i]);
		}
		
		for(int i=0; i<10; i++)
		{
			counter[i].c = 'A'+i;
			counter[i].weight = 0;
		}
		bool isGoOn = true;
		for(int i=0; i<12&&isGoOn; i++)
		{
			isGoOn = false;
			
			for(int j=0; j<n; j++)	
			{
				if(strLeng[j]-1 < i)
					continue;
				else
				{
					isGoOn = true;
					double wei = pow((double)10,(double)i);
					int index = str[j][strLeng[j]-i-1] - 'A';
					counter[index].weight += wei;
				}
			}
		}
		
		std::sort(counter,counter+10, compNode);
		
		for(int i=0; i<10; i++)
		{
			int index = counter[i].c - 'A';
			result[index] = 9-i;
			printf("%c: %d ", counter[i].c, 9-i);
		}
		printf("\n");
		res = 0;
		for(int i=0; i<n; i++)
		{
			res += str2Dou(str[i], result);
		}
		
		printf("%.0lf\n", res);
	}
	
	return 0;
	
}

